package com.bw.gmall.realtime.dws.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.common.base.BaseApp;
import com.bw.gmall.realtime.common.bean.TrafficHomeDetailPageViewBean;
import com.bw.gmall.realtime.common.constant.Constant;
import com.bw.gmall.realtime.common.util.DateFormatUtil;
import com.bw.gmall.realtime.common.util.FlinkSinkUtil;
import com.bw.gmall.realtime.dws.function.DorisMapFunction;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/*
12.3流量域首页、详情页页面浏览各窗口汇总表
 */

public class DwsTrafficHomeDetailPageViewWindow extends BaseApp {
    public static void main(String[] args) {
        new DwsTrafficHomeDetailPageViewWindow().start(Constant.TOPIC_DWD_TRAFFIC_PAGE,
                Constant.DWS_TRAFFIC_HOME_DETAIL_PAGE_VIEW_WINDOW, 4,10023);
    }

    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> dataStreamSource) {
        // 1.数据清洗
        SingleOutputStreamOperator<TrafficHomeDetailPageViewBean> beanStream = parseToPojo(dataStreamSource);
        // 2.开窗聚合
        SingleOutputStreamOperator<TrafficHomeDetailPageViewBean> resultStream = windowAndAgg(beanStream);
        resultStream.print();

        // 3.写入到Doris
        resultStream.map(new DorisMapFunction<>())
                .sinkTo(FlinkSinkUtil.getDorisSink("dws_traffic_home_detail_page_view_window"));
    }

    private static SingleOutputStreamOperator<TrafficHomeDetailPageViewBean>  windowAndAgg(SingleOutputStreamOperator<TrafficHomeDetailPageViewBean> beanStream) {
        return beanStream.assignTimestampsAndWatermarks(WatermarkStrategy
                        .<TrafficHomeDetailPageViewBean>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                        .withTimestampAssigner((event, timestamp) -> event.getTs())
                        .withIdleness(Duration.ofSeconds(120)))
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<TrafficHomeDetailPageViewBean>() {
                            @Override
                            public TrafficHomeDetailPageViewBean reduce(TrafficHomeDetailPageViewBean x, TrafficHomeDetailPageViewBean y) throws Exception {
                                x.setHomeUvCt(x.getHomeUvCt() + y.getHomeUvCt());
                                x.setGoodDetailUvCt(x.getGoodDetailUvCt() + y.getGoodDetailUvCt());
                                return x;
                            }
                        },
                        new ProcessAllWindowFunction<TrafficHomeDetailPageViewBean, TrafficHomeDetailPageViewBean, TimeWindow>() {
                            @Override
                            public void process(ProcessAllWindowFunction<TrafficHomeDetailPageViewBean, TrafficHomeDetailPageViewBean, TimeWindow>.Context context,
                                                Iterable<TrafficHomeDetailPageViewBean> iterable,
                                                Collector<TrafficHomeDetailPageViewBean> collector) throws Exception {
                                TrafficHomeDetailPageViewBean bean = iterable.iterator().next();
                                bean.setStt(DateFormatUtil.tsToDateTime(context.window().getStart()));
                                bean.setEdt(DateFormatUtil.tsToDateTime(context.window().getEnd()));
                                bean.setCurDate(DateFormatUtil.tsToDateTime(System.currentTimeMillis()));
                                collector.collect(bean);
                            }
                        });
    }

    private static SingleOutputStreamOperator<TrafficHomeDetailPageViewBean> parseToPojo(DataStreamSource<String> dataStreamSource) {
        return dataStreamSource.map(JSON::parseObject)
                .keyBy(x -> x.getJSONObject("common").getString("mid"))
                .process(new KeyedProcessFunction<String, JSONObject, TrafficHomeDetailPageViewBean>() {
                    ValueState<String> homeState;

                    ValueState<String> goodDetailState;

                    @Override
                    public void open(Configuration parameters) {
                        homeState = getRuntimeContext().getState(
                                new ValueStateDescriptor<>("homeState", String.class));

                        goodDetailState = getRuntimeContext().getState(
                                new ValueStateDescriptor<>("goodDetailState", String.class));
                    }

                    @Override
                    public void processElement(JSONObject jsonObject, KeyedProcessFunction<String, JSONObject, TrafficHomeDetailPageViewBean>.Context context, Collector<TrafficHomeDetailPageViewBean> collector) throws Exception {
                        String pageId = jsonObject.getJSONObject("page").getString("page_id");
                        Long ts = jsonObject.getLong("ts");
                        String curDate = DateFormatUtil.tsToDate(ts);
                        String lastHomeValue = homeState.value();
                        String lastGoodDetailValue = goodDetailState.value();
                        Long homeUvCt = 0L;
                        Long goodDetailUvCt = 0L;
                        if ("home".equals(pageId) && !curDate.equals(lastHomeValue)) {
                            homeUvCt = 1L;
                            homeState.update(curDate);
                        } else if ("good_detail".equals(pageId) && !curDate.equals(lastGoodDetailValue)) {
                            goodDetailUvCt = 1L;
                            goodDetailState.update(curDate);
                        }
                        if (homeUvCt + goodDetailUvCt == 1) {
                            collector.collect(
                                    new TrafficHomeDetailPageViewBean("", "", "",
                                            homeUvCt, goodDetailUvCt, ts));
                        }
                    }
                });
    }

}
